package com.practice.bootdemo.config;

import com.practice.bootdemo.eventlistener.RedisMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * redis消息队列配置-订阅者
 *
 * @author 吴彭正
 * @date 2019/03/29
 */
@Configuration
@EnableCaching
public class RedisSubscriberConfig {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisMessageListener redisMessageListener;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    /**
     * 创建连接工厂
     *
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(stringRedisTemplate.getConnectionFactory());
        container.addMessageListener(redisMessageListener,repayTopic());
        container.setTaskExecutor(threadPoolTaskExecutor);
        return container;
    }
 
    @Bean
    ChannelTopic repayTopic() {
        return new ChannelTopic("redis_channel_one");
    }
 
 
}